cmake_minimum_required(VERSION 3.5)
project(robot_grpc_proto)

#Sets common environment
include(./common.cmake)

# Finds packages
find_package(ament_cmake REQUIRED)
find_package(Protobuf REQUIRED)
find_package(gRPC CONFIG REQUIRED)

# Sets proto file path
get_filename_component(robot_proto "./proto/robot.proto" ABSOLUTE)
get_filename_component(robot_proto_path "${robot_proto}" PATH)

# Generats sources
set(robot_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/robot.pb.cc")
set(robot_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/robot.pb.h")
set(robot_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/robot.grpc.pb.cc")
set(robot_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/robot.grpc.pb.h")
add_custom_command(
      OUTPUT "${robot_proto_srcs}" "${robot_proto_hdrs}" "${robot_grpc_srcs}" "${robot_grpc_hdrs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${robot_proto_path}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${robot_proto}"
      DEPENDS "${robot_proto}")

# Includes generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# Adds robot grpc library
add_library(robot_grpc_proto SHARED
  ${robot_grpc_srcs}
  ${robot_grpc_hdrs}
  ${robot_proto_srcs}
  ${robot_proto_hdrs})
target_link_libraries(robot_grpc_proto
  absl::check
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})

target_include_directories(robot_grpc_proto PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
  )

# Installs include files
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/
        DESTINATION include/${PROJECT_NAME}
        FILES_MATCHING PATTERN "*.h")

# Installs library
install(TARGETS robot_grpc_proto
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
)

ament_package()
